package com.zking.frame;

import com.zking.dao.FoodDao;
import com.zking.entity.Address;
import com.zking.entity.Food;
import com.zking.utils.Tool工具;

import javax.swing.*;
import javax.swing.border.MatteBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Orderj extends JDialog{
    JPanel jPanel;
    double money;
    public Orderj(JFrame owner, boolean modal, Map<String,Integer> map, List<Address> addresses, String sort, String shop){

        super(owner,modal);
        // 设置名字
        setTitle("吃了没");
        // 设置窗口大小
        setSize(650, 1000);
        // 取消布局
        setLayout(null);
        // 设置居中
        setLocationRelativeTo(null);
        // 设置窗口不可调节
        setResizable(false);

        // 设置窗口关闭模式
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        JButton jButton = new JButton("返回");
        jButton.setBounds(10,10,60,30);
        add(jButton);
        jButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                dispose();
            }
        });

        jPanel = new JPanel();
        jPanel.setBounds(10,50,610,140);
        jPanel.setBackground(Color.white);
        jPanel.setLayout(null);
        JLabel jLabel = new JLabel("地址：");
        jLabel.setBounds(10,10,80,60);
        jLabel.setForeground(Color.RED);
        jLabel.setFont(new Font("黑体",Font.BOLD,20));
        jPanel.add(jLabel);
        JLabel jLabel1 = new JLabel(addresses.get(0).getAddress());
        jLabel1.setBounds(90,10,420,60);
        jLabel1.setFont(new Font("黑体",Font.BOLD,20));
        jPanel.add(jLabel1);
        JLabel jLabel2 = new JLabel("姓名：");
        jLabel2.setBounds(10,80,80,60);
        jLabel2.setForeground(Color.RED);
        jLabel2.setFont(new Font("黑体",Font.BOLD,20));
        jPanel.add(jLabel2);
        JLabel jLabel3 = new JLabel(addresses.get(0).getName());
        jLabel3.setBounds(90,80,130,60);
        jLabel3.setFont(new Font("黑体",Font.BOLD,20));
        jPanel.add(jLabel3);
        JLabel jLabel4 = new JLabel("电话号码：");
        jLabel4.setBounds(220,80,120,60);
        jLabel4.setForeground(Color.RED);
        jLabel4.setFont(new Font("黑体",Font.BOLD,20));
        jPanel.add(jLabel4);
        JLabel jLabel5 = new JLabel(addresses.get(0).getPhone());
        jLabel5.setBounds(340,80,150,60);
        jLabel5.setFont(new Font("黑体",Font.BOLD,20));
        jPanel.add(jLabel5);

        jPanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount()==1){
                    Tool工具 tool工具 = new Tool工具();
                    tool工具.setOrderj(jPanel);
                    new OrderForm(Orderj.this,true,"跟换地址",addresses);
                    jPanel.removeAll();
                    jPanel.add(tool工具.getOrderj());
                    jPanel.repaint();
                    jPanel.revalidate();
                }
            }
        });

        JPanel jPanel1 = new JPanel();
        jPanel1.setBounds(0,0,590,750);
        jPanel1.setPreferredSize(new Dimension(590,10+150*map.keySet().size()));
        jPanel1.setLayout(null);
        List<Food> foods = new ArrayList<Food>();
        FoodDao foodDao = new FoodDao();
        foods = foodDao.queryFoodName(sort,shop);
        int b=0;
        for (String key:map.keySet()){
            for (int a = 0; a<foods.size();a++){
                if (foods.get(a).getName().equals(key)){
                    JPanel jPanel = new JPanel();
                    jPanel.setBounds(0,10+150*b,600,140);
                    jPanel.setLayout(null);
                    jPanel.setBackground(Color.white);
                    JLabel jLabel61 = new JLabel("商品名称");
                    jLabel61.setBounds(10,10,400,60);
                    jLabel61.setForeground(Color.gray);
                    jLabel61.setFont(new Font("黑体",Font.BOLD,20));
                    jPanel.add(jLabel61);
                    JLabel jLabel62 = new JLabel(key);
                    jLabel62.setBounds(10,70,400,60);
                    jLabel62.setFont(new Font("黑体",Font.BOLD,30));
                    jPanel.add(jLabel62);
                    JLabel jLabel71 = new JLabel("单价");
                    jLabel71.setBounds(410,10,150,60);
                    jLabel71.setForeground(Color.gray);
                    jLabel71.setFont(new Font("黑体",Font.BOLD,20));
                    jPanel.add(jLabel71);
                    JLabel jLabel72 = new JLabel("￥"+foods.get(a).getPrice());
                    jLabel72.setBounds(410,70,130,60);
                    jLabel72.setForeground(Color.red);
                    jLabel72.setFont(new Font("黑体",Font.BOLD,20));
                    jPanel.add(jLabel72);
                    JLabel jLabel81 = new JLabel("数量");
                    jLabel81.setBounds(540,10,50,60);
                    jLabel81.setForeground(Color.gray);
                    jLabel81.setFont(new Font("黑体",Font.BOLD,20));
                    jPanel.add(jLabel81);
                    JLabel jLabel82 = new JLabel(map.get(key)+"");
                    jLabel82.setBounds(540,70,50,60);
                    jLabel82.setForeground(Color.red);
                    jLabel82.setFont(new Font("黑体",Font.BOLD,20));
                    jPanel.add(jLabel82);
                    b++;
                    jPanel1.add(jPanel);

                }
            }
        }
        JScrollPane jScrollPane = new JScrollPane(jPanel1);
        jScrollPane.setBounds(10,200,610,700);
        add(jScrollPane);

        JLabel jLabel6 = new JLabel("提交订单",0);
        jLabel6.setBounds(515,910,100,50);
        jLabel6.setFont(new Font("黑体",Font.BOLD,20));
        jLabel6.setBorder(new MatteBorder(2,2,2,2,Color.BLUE));
        add(jLabel6);
        for(String key:map.keySet()){
            for (int ab = 0;ab<foods.size();ab++){
                if (foods.get(ab).getName().equals(key)){
                    money += foods.get(ab).getPrice()*map.get(key);
                    break;
                }
            }
        };
        JLabel jLabel7 = new JLabel("￥"+0,SwingConstants.RIGHT);
        DecimalFormat df1 = new DecimalFormat("####.00");
        jLabel7.setText("￥"+df1.format(money));
        jLabel7.setForeground(Color.red);
        jLabel7.setFont(new Font("黑体",Font.BOLD,20));
        jLabel7.setBounds(10,910,505,50);
        add(jLabel7);
        jLabel6.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount()==1){
                    Tool工具 tool工具 = new Tool工具();
                    int a=tool工具.setOrders(tool工具.getName(),shop,df1.format(money));
                    if (a==1){
                        JOptionPane.showMessageDialog(Orderj.this,"亲 你的订单已送达，记得五星好评哟");
                        dispose();
                    }else{
                        JOptionPane.showMessageDialog(Orderj.this,"亲 你的订单出现错误");
                        dispose();
                    }
                }
            }
        });



        add(jPanel);
        // 设置窗口是否可见
        setVisible(true);
        // 加载并刷新面板

        repaint();
        revalidate();
    }

}
